package com.hsy.linemoney.modules.auth.activity;

import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.hsy.linemoney.R;
import com.hsy.linemoney.apiservice.Callback;
import com.hsy.linemoney.apiservice.LineMoneyContext;
import com.hsy.linemoney.apiservice.RegistrationService;
import com.hsy.linemoney.modules.common.CommonBaseActivityWithSimpleActionBar;
import com.hsy.linemoney.modules.common.EditTextExt;
import com.hsy.linemoney.modules.main.MainActivity;
import com.hsy.linemoney.requests.RequestTask;

public class VerifyEmailActivity extends CommonBaseActivityWithSimpleActionBar implements View.OnClickListener {
    protected EditTextExt emailVerifyCode;
    protected TextView resendCode;
    protected TextView modifyEmailAddress;
    protected TextView verifyImmediately;

//    @Override
//    public int getRightbuttonTextResId() {
//        return R.string.txt_save;
//    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.activity_verify_email);
        setTitle(R.string.txt_verify_emailaddress);
//        setRightButtonTextColor(R.color.white);
        initView();

    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.resend_code) {
            final Snackbar snackbar = Snackbar.make(getRootView(), R.string.txt_verify_code_sending, Snackbar.LENGTH_INDEFINITE);
            snackbar.show();
            RegistrationService service = new RegistrationService();
            service.sendEmailAddressVerification(LineMoneyContext.getLoggedUser().getEmailAddress(), new Callback<RegistrationService.SendEmailAddressVerificationResult>() {
                @Override
                public void onSuccess(int what, RegistrationService.SendEmailAddressVerificationResult result) {
                    showToast(R.string.txt_send_successfully_please_check);
                    snackbar.setText(R.string.txt_verify_code_sent);
                    snackbar.setDuration(Snackbar.LENGTH_SHORT);
                    snackbar.show();
                }

                @Override
                public void onError(int what, int type, String msg, Exception e) {
                    snackbar.dismiss();
                }
            });
        } else if (view.getId() == R.id.modify_email_address) {
            startActivity(UpdateEmailAddressActivity.class);
        } else if (view.getId() == R.id.verify_immediately) {
            if (TextUtils.isEmpty(emailVerifyCode.getText())) {
                showToast(R.string.txt_pleane_verify_code);
                return;
            }
            RegistrationService service = new RegistrationService();
            service.verifyEmailAddressWithKey(emailVerifyCode.getText().toString(), new Callback<RegistrationService.VerifyEmailAddressWithKeyResult>() {
                @Override
                public void onSuccess(int what, RegistrationService.VerifyEmailAddressWithKeyResult result) {
                    showToast(R.string.txt_email_verified);
                    startActivity(AgreementActivity.class, LineMoneyContext.getLoggedUser().getEmailAddress());
                }

                @Override
                public void onError(int what, int type, String msg, Exception e) {

                }
            });
        }
    }

    private void initView() {
        emailVerifyCode = (EditTextExt) findViewById(R.id.email_verify_code);
        resendCode = (TextView) findViewById(R.id.resend_code);
        resendCode.setOnClickListener(VerifyEmailActivity.this);
        modifyEmailAddress = (TextView) findViewById(R.id.modify_email_address);
        modifyEmailAddress.setOnClickListener(VerifyEmailActivity.this);
        verifyImmediately = (TextView) findViewById(R.id.verify_immediately);
        verifyImmediately.setOnClickListener(VerifyEmailActivity.this);
    }
}
